/*
 * @Description: 
 * @Author: yyh
 * @Date: 2022-03-23 23:53:54
 * @LastEditors: yyh
 * @LastEditTime: 2022-03-23 23:53:57
 */
// 导出Excel表格

// 引入file-saver和xlsx依赖
import FileSaver from "file-saver"
import XLSX from "xlsx"

const utilway = {
    // 导出excel表格方法
    exportExcel(id,name){
      // 设置当前日期
      let time = new Date();
      let year = time.getFullYear();
      let month = time.getMonth() + 1;
      let day = time.getDate();
      // excel表格名字
      let excelName = year + "." + month + "." + day + "-" + `${name}`;
      // 只导出不解析
      let xlsxParam = { raw: true};
      var wb = XLSX.utils.table_to_book(document.querySelector("#"+id),xlsxParam);
      // 获取二进制字符串作为输出
      var wbout = XLSX.write(wb,{
          bookType: "xlsx",
          bookSST: true,
          type: "array"
      })
      try{
          //  excelName+'.xlsx'表示导出的excel表格名字
          FileSaver.saveAs(
              new Blob([wbout],{ type: "application/octet-stream" }),
              excelName + ".xlsx"
          );
      } catch (e) {
          if(typeof console !== "undefined") {
              console.log(e,wbout);
          }
      }
      return wbout;
    }
}

export default utilway;